﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using System.Web.UI.HtmlControls;
using BLL.Common;
using BLL;

public partial class UserControl_MenuControl : System.Web.UI.UserControl
{
    List<MenuInfo> modules = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        modules = BMenu.getInstance().GetAllEnum();
        List<MenuInfo> modulesRoot = modules.FindAll(FindMenuRoot);
        rptMenuRoot.DataSource = modulesRoot;
        rptMenuRoot.DataBind();
    }

    private bool FindMenuRoot(MenuInfo m)
    {
        if (m.ParentId == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private List<MenuInfo> FindMenuChild(List<MenuInfo> source, int parentId)
    {
        List<MenuInfo> result = new List<MenuInfo>();

        if (source != null && source.Count > 0)
        {
            for (int i = 0; i < source.Count; i++)
            {
                if (source[i].ParentId == parentId)
                {
                    result.Add(source[i]);
                }
            }
        }

        return result;
    }

    protected void rptMenuRoot_ItemDataBound(object source, RepeaterItemEventArgs e)
    {
        Repeater rptMenuChild = (Repeater)e.Item.FindControl("rptMenuChild");
        Literal litModuleId = (Literal)e.Item.FindControl("litModuleId");
        if (rptMenuChild != null && litModuleId != null)
        {
            rptMenuChild.DataSource = FindMenuChild(modules, Convert.ToInt32(litModuleId.Text));
            rptMenuChild.DataBind();
        }
    }

    protected void rptMenuChild_ItemDataBound(object source, RepeaterItemEventArgs e)
    {
        HtmlAnchor link = (HtmlAnchor)e.Item.FindControl("a_link");
        if (link != null)
        {

            link.HRef = Contexts.AdminPath + ((MenuInfo)e.Item.DataItem).Path;
           
        }
    }
}